package com.iot.service;

import com.iot.mapper.DTdeptMapper;
import com.iot.mapper.TdeptMapper;
import com.iot.pojo.MTdept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class TdeptServiceImpl implements TdeptService {
    @Autowired
    private TdeptMapper mapper;
    @Autowired
    private DTdeptMapper dmapper;

    @Override
    public int getRecordCount(String sWhere) {
        return mapper.getRecordCount(sWhere);
    }

    @Override
    public int insert(MTdept mTdept) throws Exception {
        int count = 0;
        count = getRecordCount(" AND SNAME='" + mTdept.getSNAME() + "' AND APP_ID ="+ mTdept.getAPP_ID()+"");
        if (count > 0)
            throw new Exception("该名称已存在");
        return mapper.insert(mTdept);
    }

    @Override
    public void update(MTdept mTdept) throws Exception {
        int count = 0;
        count = getRecordCount(" AND APP_ID ="+ mTdept.getAPP_ID()+" AND SNAME='" + mTdept.getSNAME() + "' AND DEPT_ID!=" + mTdept.getDEPT_ID() + "");
        if (count > 0)
            throw new Exception("该名称已存在");
        mapper.update(mTdept);
    }

    @Override
    public void deleteById(int id) throws Exception {

       List<MTdept> list=fetch(" AND PID='"+ id+"'");
       if(list.size()>0)
       {
           throw new Exception("存在下级部门不能删除！");
       }
        mapper.deleteById(id);
    }

    @Override
    public MTdept single(Integer id) {
        return mapper.single(id);
    }

    @Override
    public List<MTdept> fetch(String sWhere) {
        return mapper.fetch(sWhere);
    }

    @Override
    public List<Map<String,Object>> fetchByPage(Map<String, Object> map) {
        return dmapper.fetchByPage(map);
    }

}
